// const {
//     exec,
//     escape
// } = require('../db/mysql')

const {
    getDynamicLike,
    createDynamicLike,
    deleteDynamicLike,
    updateDynamic,
    updateDynamicLike
} = require('../services/dynamic')
const {
    SuccessModel,
    ErrorModel
} = require('../model/ResModel')
const {
    DynamicCancelLike
} = require('../model/ErrorInfo')
/**
 * @description 动态点赞
 * @param {*} param0 
 */
async function dynamicLike({
    dynamicId,
    userId
}) {
    const result = await getDynamicLike({ //查询动态喜欢是否有当前数据
        dynamicId,
        userId
    })
    if (result == null) { //数据库没有数据
        const addLike = await createDynamicLike({
            dynamicId,
            userId
        })
        updateDynamicLikeNum(1, dynamicId)
        return new SuccessModel({
            msg: '点赞成功!'
        })
    }
    if (result.isLike == 0) { //数据库有数据但是未点赞
        await updateDynamicLike({
            dynamicId,
            userId,
            isLike: 1
        })
        updateDynamicLikeNum(1, dynamicId)
        return new SuccessModel({
            msg: '点赞成功!'
        })
    }
    if (result.isLike == 1) { //判断已点赞 取消点赞
        await updateDynamicLike({
            dynamicId,
            userId,
            isLike: 0
        })
        updateDynamicLikeNum(0, dynamicId)
        return new ErrorModel(DynamicCancelLike)
    }
    // let sql = `
    //             select * from dynamic_like where dynamic_id = ${escape(dynamic_id)} and uid = ${escape(uid)}
    // `
    // let listData = await exec(sql)
    // if(listData != ''){
    //     is_like = listData[0].is_like == 0 ? 1 :0
    //     let sql = ` update  dynamic_like set is_like=${is_like} where id=${listData[0].id}`
    //     const update = await exec(sql)
    //     if(update.affectedRows > 0){//受影响行数
    //         const msg = is_like == 0 ? '取消点赞成功' : '点赞成功'
    //         dynamicAddNums(is_like,listData[0].dynamic_id)
    //         return {
    //             msg
    //         }
    //     }
    // }else{
    //     let createtime = (new Date()).valueOf()
    //     let sql = `insert into dynamic_like (dynamic_id,uid,createtime) values (${escape(dynamic_id)},${escape(uid)},${createtime})`
    //     const insert = await exec(sql)
    //     if(insert.affectedRows > 0){
    //         return {
    //             msg:'点赞成功'
    //         }
    //     }
    // }
}

/**
 * @description 点赞成功
 * @param {*} dynamicId 
 */
function likeSucess(dynamicId) {
    updateDynamicLikeNum(1, dynamicId)
    return new SuccessModel({
        msg: '点赞成功!'
    })
    console.log(12634);
}

/**
 * @description 增加减少动态likeNum 数量
 * @param {number} condition 1为增加动态列表likeNum0为减少
 */
function updateDynamicLikeNum(condition, dynamicId) {
    updateDynamic(condition, dynamicId) //更新动态列表点赞数量
}

// const dynamicAddNums = async (is_like,id)=>{
//     let numsSql = `select like_nums from dynamic where id=${id};`
//     let num = await exec(numsSql)
//     num = num[0].like_nums
//     num = is_like == 0 ? num-=1 : num+=1
//     let sql = `
//         update dynamic set like_nums='${num}' where id=${id};
//     `
//     const updata =  await exec(sql)
// }

module.exports = {
    dynamicLike
}